(:
  A procedure to select the contents of coffee threaded chat interventions
  across several versions (alpha+, 1.6 and 1.7?, 2.0).
  
  all items contain infos:
  	software
  	tool
  	step
  	time
  	src-anchor
  	group
  	user
  	type
  	content
  	- add message, edit message, delete message, move message, editing message, set tag
  		category
  	- add category, rename category 
  		categoryname
  		categoryid
  	- move category, delete category
  		categoryid
  	- add message, settag
  		tag
  	- add message
  		parentid  
  		messageid --- tentative calculation except in alpha+
  	- edit message, delete message, move message, editing message, set tag
  		contributionid
  	- begin edit message ---- I suspect this doesn't belong here, but in normal chat
  		starting = ???
  	- move message
  		fromcategory
  		tocategory 
  	- move category
  		direction (up|down)
  	- change anonymity
  		anonymous
  		isanonymous (identical but explicitely boolean)  
  		
  		
  		
  		TODO --- add type "import message", 
  		--- handle blocking and group changes
  		--- change into several smaller scripts
  		--- move where clause into xpath - script will run faster
:)

declare boundary-space strip;
import module namespace
    jj = "http://kumquat.emse.fr/utilitaires"
    at "jjutils.xq" ;
import module namespace
    coffee = "http://tatiana.emse.fr/coffee"
    at "coffee-utils.xq" ;
    
declare function local:format-category-name($name as xs:string*) as xs:string* {
	fn:substring-before(fn:substring-after($name,"("),")")
};

declare function local:format-tag-name($tag as xs:string*) as xs:string* {
        if (fn:substring($tag,1,5) eq "lead.") then
		fn:substring-before(fn:substring-after($tag,"strTag="),";")
	else
		$tag
};

<display>{

let $t := $arguments[1]
let $doc := doc($t)
let $version := coffee:get-version($doc)
let $msgs := coffee:get-messages($doc)
let $newmsgs := $msgs[.//(MLThreadedChatMessage|MLThreadedChatServerMessage|MLTCAddCategoryServerMessage|MLTCAddCategoryMessage)]
for $msg at $p in $msgs
let $chat := $msg//(MLThreadedChatMessage|MLThreadedChatServerMessage)
let $category := $msg//(MLTCAddCategoryServerMessage|MLTCAddCategoryMessage)
let $delcat := $msg//MLTCDeleteCategoryMessage
let $edit := $msg//MLTCEditMessage
let $editing := $msg//MLTCEditingMessage
let $move := $msg//MLTCMoveBranchMessage
let $movecat := $msg//MLTCMoveCategoryMessage
let $settag := $msg//MLTCSetContribTagMessage
let $delete := $msg//MLThreadedChatDeleteMessage
let $anon := $msg//MLThreadedChatMalleabilityMessage
let $renamecat := $msg//MLTCRenameCategoryMessage
let $disable := $msg//DisableServiceMessage
let $groupctrl := $msg//ClientGroupControlSrvMsg
let $block := $msg//BlockedServiceMessage
let $unblock := $msg//unBlockedServiceMessage


  where (coffee:get-tool($msg)="mlThreadedChatTool")
  return
  if (($version eq 104) and $msg//MLThreadedChatMessage) then (:exclude duplicate tc messages:)
	()
  else if (($version eq 100) and ($msg//@UserName = "SERVER") and ($msg//@from != "Teacher")) then
  	()
  else
  <item>{ 
  
  		<info name="software">{"coffee:softwarename"}</info>,
      <info name="tool">threadedChat</info>,
      <info name="time"><time><date>{coffee:get-timemillis($msg)}</date></time></info>,
    	<info name="src-anchor"><anchor>{
      	<doc>{ $t }</doc>,
      	<path>{jj:build-Path($msg)}</path>
    	}</anchor></info>,
      <info name="group">{coffee:get-group($msg)}</info>,
      <info name="step">{coffee:get-step($msg)}</info>,
      <info name="user">{coffee:get-username($msg)}</info>,
      if ($chat) then
      	(<info name="type">add message</info>,
      	<info name="content"><text>{fn:string-join((coffee:string($chat/@text),
      		if ((string($chat/@pastingRoot)="false") and (string($chat/@cloningMessage)="true")) then "(imported from previous step)" else ""),"")}</text></info>,
      	<info name="category"><text>{local:format-category-name(string($chat/@category))}</text></info>, (: actual cat is not stored anywhere:)
      	<info name="tag"><text>{local:format-tag-name(string($chat/@contributionTag))}</text></info>, (: not present in some versions :)
      	<info name="parentid">{string($chat/@parentUid)}</info>,
      	<info name="messageid">{
      		if ($version le 104) then
      			string($chat/@uid)
      		else
      		let $id := for $m at $position in $newmsgs[coffee:get-group($msg) = coffee:get-group(.)][coffee:get-step($msg) = coffee:get-step(.)]
      					return if ($m is $msg) then ($position) else ()
      					
      		return $id
      	}</info>)
      else if ($category) then
      	(<info name="type">add category</info>,
      	<info name="content">created a category</info>,
      	<info name="categoryname">{string($category/@categoryName)}</info>,
      	<info name="categoryid">{string($category/@categoryID)}</info>)
      else if ($delcat) then
        (<info name="type">delete category</info>,
      	<info name="content">deleted a category</info>,
      	<info name="categoryname">{string($delcat/@categoryName)}</info>, (: get the real name somewhere :)
      	<info name="categoryid">{string($delcat/@categoryID)}</info>)
      else if ($edit) then
      	(<info name="type">edit message</info>,
      	<info name="content"><text>{coffee:string($edit/@text)}</text></info>,
      	<info name="category"><text>{string($edit/@category)}</text></info>,
      	<info name="contributionid">{string($edit/@contributionID)}</info>)
      else if ($editing) then
        (<info name="type">begin edit message</info>,
      	<info name="content"><text>{"editing message"}</text></info>,
      	<info name="category"><text>{string($editing/@category)}</text></info>,
      	<info name="contributionid">{string($editing/@itemID)}</info>,
      	<info name="starting">{string($editing/@starting)}</info>) (:wtf mate?:)
      else if ($move) then
      	(<info name="type">move branch</info>,
      	<info name="content"><text>{"move branch from",string($move/@categFrom),"to",string($move/@categTo)}</text></info>,
      	<info name="contributionid"><text>{string($move/@branchRoot)}</text></info>,
      	<info name="fromcategory"><text>{string($move/@categFrom)}</text></info>,
      	<info name="tocategory">{string($move/@categTo)}</info>)
      else if ($movecat) then
      	(<info name="type">move category</info>,
      	<info name="content"><text>{"move category",string($movecat/@direction)}</text></info>,
      	<info name="categoryname"><text>{string($movecat/@category)}</text></info>,
      	<info name="direction">{string($movecat/@direction)}</info>)
      else if ($settag) then
      	(<info name="type">set tag</info>,
      	<info name="content"><text>{"set tag to:",local:format-tag-name(string($settag/@contributionTag))}</text></info>,
      	<info name="tag"><text>{local:format-tag-name(string($settag/@contributionTag))}</text></info>,
      	<info name="category"><text>{string($settag/@category)}</text></info>,
      	<info name="contributionid">{string($settag/@contributionID)}</info>)
      else if ($delete) then
      	(<info name="type">delete branch</info>,
      	<info name="content"><text>{"delete branch"}</text></info>,
      	<info name="category"><text>{string($delete/@category)}</text></info>,
      	<info name="contributionid">{string($delete/@branchRoot)}</info>)
      else if ($anon) then
      	(<info name="type">change anonymity</info>,
      	<info name="content"><text>{"chat is",if ($anon/@anonymous="true") then "now anonymous" else "no longer anonymous"}</text></info>,
      	<info name="anonymous"><text>{string($anon/@anonymous)}</text></info>,
      	<info name="isanonymous"><boolean>{boolean($anon/@anonymous = "true")}</boolean></info>)
      else if ($renamecat) then
      	(<info name="type">rename category</info>,
      	<info name="content"><text>{"rename category"}</text></info>,
      	<info name="categoryid"><text>{string($renamecat/@categoryID)}</text></info>,
      	<info name="categoryname">{string($renamecat/@categoryNewName)}</info>)
      else if ($disable) then
      	(<info name="type">disable service</info>,
      	<info name="content"><text>{"threaded chat disabled"}</text></info>)
      else if ($groupctrl) then
      	(<info name="type">change group</info>,
      	<info name="content"><text>{string($groupctrl/@client),"was moved from group", string($groupctrl/@previousGroup), "to group", string($groupctrl/@idGroup)}</text></info>,
      	<info name="userid">{string($groupctrl/@client)}</info>,
      	<info name="groupfrom">{string($groupctrl/@previousGroup)}</info>,
      	<info name="groupto">{string($groupctrl/@idGroup)}</info>,
      	<info name="ischange"><boolean>{string($groupctrl/@isGroupChange)}</boolean></info>)
      else if ($block) then
      	(<info name="type">block</info>,
      	<info name="content"><text>{"threaded chat blocked for",string($block/@username)}</text></info>,
      	<info name="userid">{string($block/@username)}</info>)
      else if ($unblock) then
      	(<info name="content"><text>{"threaded chat unblocked for",string($unblock/@username)}</text></info>,
      	<info name="userid">{string($unblock/@username)}</info>)
      else
      	<info name="content">{"unknown threadedchat message:", name($msg/ToolMessage/*),"please report this bug"}</info>
  }</item>
}</display>
